<?php

namespace app\admin\controller\product;

use app\common\controller\Backend;

/**
 * 
 * 商品回收站管理
 * 
 */
class Productrecycle extends Backend
{
    protected $relationSearch = true;
    protected $Typemodel = null;
    protected $Unitmodel = null;
    public function _initialize()
    {
        parent::_initialize();

        $this->model = model('Product.Product');
        $this->Unitmodel = model('Product.Unit');
        $this->Typemodel = model('Product.Type');
        $unit = $this->Unitmodel->order('id', 'desc')->select();
        $type = $this->Typemodel->order('weigh', 'desc')->select();
        $unitdata = [];
        $typedata = [];
        foreach ($unit as $item) {
            $unitdata[$item['id']] = $item['name'];
        }
        foreach ($type as $item) {
            $typedata[$item['id']] = $item['name'];
        }
        $rentstatusdata = [
            '1' => '普通商品',
            '2' => '租赁商品只能租',
            '3' => '既购买也租赁的商品'
        ];
        $flagdata = [
            '1' => '新品',
            '2' => '热销',
            '3' => '推荐'
        ];
        $this->assign(['unit' => $unitdata, 'type' => $typedata, 'rentstatus' => $rentstatusdata, 'flag' => $flagdata]);
    }
    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->onlyTrashed()
                ->with(['type', 'unit'])
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->onlyTrashed()
                ->with(['type', 'unit'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }
      /**
     * 删除
     */
    public function del($ids = null)
    {
        $row = $this->model->onlyTrashed()->select($ids);
        if (empty($row)) {
            $this->error('当前商品不存在');
        }
        $result = $this->model->destroy($ids,true);
        if (!$result) {
            $this->error($this->model->getError());
        }
        
        foreach($row as $item){
            $imgs=explode(',',$item['thumbs']);
            foreach($imgs as $img){
                is_file(ltrim($img, '/')) && unlink(ltrim($img, '/'));
                delImg($img);
            }
        }
        $this->success();
    }
     /*
    还原
    */ 
    public function restore($ids=null){
        $row = $this->model->onlyTrashed()->select($ids);
        if (empty($row)) {
            $this->error('当前商品不存在');
        }
        $res=$this->model->onlyTrashed()->where('id','IN',$ids)->update(['deletetime'=>null]);
         if($res==0){
                $this->error($this->model->getError());
        }
       $this->success();
    }
}
